﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Xiami.Helper.Model;
using AMing.Extension.Net40;
using MusicForMvc3.Method;
using System.Web.Security;
using Html5Music.Data;

namespace MusicForMvc3.Controllers
{
    public class AdminController : BaseController
    {
        public AdminController()
        {
            base.ViewPath = "~/Views/Admin/{0}.cshtml";
        }

        [AdminAuthorize]
        public ActionResult Index()
        {
            var b = HttpContext.User.Identity.IsAuthenticated;
            ViewBag.Access = this.Admin_Access;

            return ViewPage();
        }


        /// <summary>
        /// 登陆页
        /// </summary>
        /// <param name="callback"></param>
        /// <returns></returns>
        public ActionResult Login(string returnUrl)
        {
            if (IsLogin)
            {
                return Redirect("index");
            }
            ViewBag.ReturnUrl = returnUrl;
            return ViewPage();
        }


        /// <summary>
        /// 登陆验证
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="userpwd"></param>
        /// <param name="callback"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login(string userid, string userpwd, string returnUrl)
        {
            bool isLogin = false;
            if (userid == SuperAdmin_ID && userpwd == SuperAdmin_Pwd)//超级管理员登陆
            {
                isLogin = true;
                string all_access = "";
                foreach (var item in System.Enum.GetValues(typeof(UserAccess)))
                {
                    all_access += "," + item.ToString();
                }
                SetUserAccess(all_access, userid);
            }
            else
            {
                Html5Music.Model.User user = Userlist.Login(userid, userpwd);
                if (user != null)
                {
                    isLogin = true;
                    SetUserAccess(user.Access, userid);
                }
            }
            if (isLogin)
            {
                FormsAuthentication.SetAuthCookie(userid, true);

                if (string.IsNullOrWhiteSpace(returnUrl))
                {
                    returnUrl = "/admin/index";
                }

                return Redirect(returnUrl);
            }
            ViewBag.UserID = userid;
            ViewBag.Error = "账号密码有误，登陆失败！";

            return Login(returnUrl);
        }

        /// <summary>
        /// 注销
        /// </summary>
        /// <returns></returns>
        [AdminAuthorize]
        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            base.Loguot();

            return RedirectToAction("index");
        }


        #region Method
        /// <summary>
        /// 设置用户权限及账号
        /// </summary>
        /// <param name="access"></param>
        /// <param name="username"></param>
        private void SetUserAccess(string access, string username)
        {
            base.Admin_Access = string.Format("userid:{0},{1}", username, access);
        }

        #endregion
    }
}
